const Model = require('../dbs/model/getCollection.js')
const TravelRecordSchema = require('../dbs/schema/travelRecordSchema.js')
const result = require('../com/result.js')

/**
 * 查满足某些条件的出行记录
 * @param {*} ctx 
 * @param {*} next 
 */
let getRecord = async (ctx, next) => {
    let param = ctx.request.body;
    console.log('参数');
    console.log(Date.now())
    console.log(param)
    let and = [];

    let pageNum = !param.pageNum ? 0 : Number(param.pageNum) - 1;
    let size = !param.size ? 10 : Number(param.size);

    if (param.faceGroupName) {
        and.push({ faceGroupName: param.faceGroupName });
    }
    if (param.tenantId) {
        and.push({ tenantId: param.tenantId });
    }
    if (param.name) {
        and.push({ name: param.name });
    }
    if (param.deviceName) {
        and.push({ deviceName: param.deviceName });
    }
    if (param.throughType) {
        and.push({ throughType: param.throughType });
    }
    if (param.userId) {
        and.push({ userId: param.userId });
    }
    if (param.deviceId) {
        and.push({ deviceId: param.deviceId });
    }
    if (param.users) {
        // let users = JSON.parse(String(param.users));
        let users = param.users;
        let userCardNoArr = [];
        let idCardNoArr = [];
        let studentNoArr = [];
        for (let key in users) {
            let userCardNo = users[key].userCardNo;
            userCardNo ? userCardNoArr.push(userCardNo) : '';
            let idCardNo = users[key].idCardNo;
            idCardNo ? idCardNoArr.push(idCardNo) : '';
            let studentNo = users[key].studentNo;
            studentNo ? studentNoArr.push(studentNo) : '';
        }
        let userIn = [];
        userCardNoArr.length > 0 ? userIn.push({
            userCardNo: { "$in": userCardNoArr }
        }) : '';
        idCardNoArr.length > 0 ? userIn.push({
            idCardNo: { "$in": idCardNoArr }
        }) : '';
        studentNoArr.length > 0 ? userIn.push({
            studentNo: { "$in": studentNoArr }
        }) : '';
        and.push({
            "$or": userIn
        });
    }
    let timeExi = {
        recordTime: {}
    };
    if (param.startTime) {
        //数据库的时间大于等于这个时间
        let i = String(param.startTime).length > 10 ? 1 : 1000;
        let timeNum = parseInt(param.startTime, 10) * i;
        timeExi['recordTime']['$gte'] = new Date(timeNum);
    }
    if (param.endTime) {
        //数据库的时间小于这个时间
        let i = String(param.endTime).length > 10 ? 1 : 1000;
        let timeNum = parseInt(param.endTime, 10) * i;
        let endTime = new Date(timeNum);
        timeExi['recordTime']['$lt'] = endTime;
    }
    if (timeExi['recordTime']['$lt'] || timeExi['recordTime']['$gte']) {
        and.push(timeExi);
    }

    let TravekRecordModel = await Model.getComModel({
        dataBase: 'com_info',
        tableName: 'travel_record',
        schema: TravelRecordSchema
    });

    return await new Promise((res, rej) => {
        let i = 0;
        let data = {};
        let where = {};
        and.length > 0 ? where['$and'] = and : '';
        TravekRecordModel
            .find(where)
            .skip(pageNum * size)
            .limit(size)
            .sort({ recordTime: -1 })
            .exec((error, doc) => {
                i++;
                data.data = doc;
                if (i == 3) {
                    ctx.body = result.go(result.code.CODE_200, data);
                    return res();
                }
            });
        TravekRecordModel.countDocuments(where, (error, count) => {
            i++;
            data.allCount = count;
            if (i == 3) {
                ctx.body = result.go(result.code.CODE_200, data);
                return res();
            }
        });
        let unWhere = JSON.parse(JSON.stringify(where));
        unWhere['$and'] ? unWhere['$and'].push({ 'unusual': true }) : unWhere['unusual'] = true;
        TravekRecordModel.countDocuments(unWhere, (error, count) => {
            i++;
            data.allUnusualCount = count;
            if (i == 3) {
                ctx.body = result.go(result.code.CODE_200, data);
                return res();
            }
        });

    });

};

module.exports = {
    getRecord
}  